package com.tuozixuan.onesql.mysql;

import java.io.Closeable;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Objects;

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSource;

public class JdbcUtils {
    
    private static BasicDataSource datasource = new BasicDataSource();
    
    static{
        //数据库连接信息,必须的
        datasource.setDriverClassName("com.mysql.jdbc.Driver");
        datasource.setUrl("jdbc:mysql://localhost:3306/one");
        datasource.setUsername("root");
        datasource.setPassword("111111");
        //对象连接池中的连接数量配置,可选的
        datasource.setInitialSize(10);//初始化的连接数
        datasource.setMaxActive(8);//最大连接数量
        datasource.setMaxIdle(5);//最大空闲数
        datasource.setMinIdle(1);//最小空闲
    }
    
    public static DataSource getDataSource() {
        return datasource;
    }
    
    public static Connection getConnection() throws SQLException {
        return datasource.getConnection();
    }
    
    public static void close(Closeable closeable) {
        if (Objects.nonNull(closeable)) {
            try {
                closeable.close();
            } catch (IOException e) {
            }
        }
    }
}
